﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WineStore.DAL;
using WineStore.Models;
using System.Data;
using PagedList;
using PagedList.Mvc;

namespace WineStore.Controllers
{
    public class ContactUsController : Controller
    {
        //
        // GET: /ContactUs/

        public ActionResult ShowContactUs(int? page)
        {
            using (var db = new WineDbContext())
            {
                var contact = db.ContactUs
                    .OrderByDescending(n => n.ContactUsId);
                int pageSize = 10;
                int pageNumber = (page ?? 1);

                // Thiet lap thuoc tinh khi phan trang
                PagedListRenderOptions pro = new PagedListRenderOptions();
                pro.MaximumPageNumbersToDisplay = 5;
                pro.LinkToLastPageFormat = "Last";
                pro.DisplayLinkToLastPage = true;
                pro.LinkToFirstPageFormat = "First";
                pro.DisplayLinkToFirstPage = true;
                pro.LinkToNextPageFormat = ">>";
                pro.DisplayLinkToNextPage = true;
                pro.LinkToPreviousPageFormat = "<<";
                pro.DisplayLinkToPreviousPage = true;

                ViewBag.Pro = pro;

                return View(contact.ToPagedList(pageNumber, pageSize));
            }
        }

        // GET: /ContactUs/DeleteContactUs/
        public ActionResult DeleteContactUs(int id)
        {
            ContactUs contact = null;
            using (var db = new WineDbContext())
            {
                contact = db.ContactUs.Find(id);
                if (contact == null)
                {
                    return HttpNotFound();
                }
            }
            return View(contact);
        }

        // POST: /ContactUs/DeleteContactUs/
        [HttpPost, ActionName("DeleteContactUs")]
        public ActionResult DeleteConfirmed(int id)
        {
            ContactUs contact = null;
            try
            {
                using (var db = new WineDbContext())
                {
                    contact = db.ContactUs.Find(id);
                    db.Entry(contact).State = EntityState.Deleted;
                    db.SaveChanges();
                    return RedirectToAction("ShowContactUs", "ContactUs");
                }
            }
            catch (DataException)
            {
                ModelState.AddModelError("", "Unable to save changes");
            }
            return View(contact);
        }

    }
}
